package hz.mobileproject.pda.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.response.Response_meetRecord_item;
import hz.mobileproject.pda.entity.response.Response_meetRecord_item;
import hz.mobileproject.pda.network.Constance;

/**
 * Created by Lc on 2017/10/18.
 */

public class MeetRecordingAdapter extends RecyclerView.Adapter<MeetRecordingAdapter.MyViewHolder> {

    private Context mContext;
    private List<Response_meetRecord_item> listData;
    //上一个播放的position
    private int lastPlayPosition = -1;
    private OnItemClickListener onItemClickListener;

    public MeetRecordingAdapter(Context c, List<Response_meetRecord_item> listData) {
        this.mContext = c;
        this.listData = listData;
        if(this.listData == null){
            this.listData = new ArrayList<Response_meetRecord_item>();
        }
    }

    public List<Response_meetRecord_item> getListData() {
        return listData;
    }

    public void setListData(List<Response_meetRecord_item> listData) {
        if(listData == null){
            this.listData = new ArrayList<Response_meetRecord_item>();
        }else{
            this.listData = listData;
        }
        this.notifyDataSetChanged();
    }
    public void addListData(List<Response_meetRecord_item> listData){
        if(listData == null){
            return;
        }
        this.listData.addAll(listData);
        this.notifyDataSetChanged();
    }

    public void clearAll() {
        this.listData.clear();
        this.notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.layout_meet_recording_incv, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if(position == 0){
            holder.v_line.setVisibility(View.VISIBLE);
        }else{
            holder.v_line.setVisibility(View.GONE);
        }
        holder.pr_name.setText(listData.get(position).getPr_name());
        holder.pr_no.setText(listData.get(position).getPr_no());
        holder.visittype.setText(listData.get(position).getVisittype());
        holder.prenum.setText(listData.get(position).getPrenum());
        holder.state.setText(listData.get(position).getState());
        holder.visitor_info.setText(listData.get(position).getVisitor_info());
        holder.startdate.setText(listData.get(position).getStartdate());
        holder.enddate.setText(listData.get(position).getEnddate());
        //谈话录音评估
        holder.tv_remark.setText(listData.get(position).getTalk_effect_name());
        if(StringUtils.isEmpty(listData.get(position).getTalk_effect_name())){
            holder.ll_remark.setVisibility(View.GONE);
        }else{
            holder.ll_remark.setVisibility(View.VISIBLE);
            if(Constance.TALK_EFFECT_OK.equals(listData.get(position).getTalk_effect())){
                holder.tv_remark.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_remark.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }

        //情绪评估
        holder.tv_emotion.setText(listData.get(position).getEmotion_evaluate_name());
        if(StringUtils.isEmpty(listData.get(position).getEmotion_evaluate_name())){
            holder.ll_emotion.setVisibility(View.GONE);
        }else{
            holder.ll_emotion.setVisibility(View.VISIBLE);
            if(Constance.EMOTION_EVALUATE_OK.equals(listData.get(position).getEmotion_evaluate())){
                holder.tv_emotion.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_emotion.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }

        //危险评估
        holder.tv_danger.setText(listData.get(position).getDanger_evaluate_name());
        if(StringUtils.isEmpty(listData.get(position).getDanger_evaluate_name())){
            holder.ll_danger.setVisibility(View.GONE);
        }else{
            holder.ll_danger.setVisibility(View.VISIBLE);
            if(Constance.DANGER_EVALUATE_OK.equals(listData.get(position).getDanger_evaluate())){
                holder.tv_danger.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_danger.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }

        holder.ll_rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.OnItemClick(position,listData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView pr_no;
        private TextView pr_name;
        private TextView visittype;
        private TextView prenum;
        private TextView state;
        private TextView visitor_info;
        private TextView startdate;
        private TextView enddate;
        private LinearLayout ll_rootView;
        private View v_line;
        private TextView tv_remark;
        private LinearLayout ll_remark;
        private TextView tv_emotion;
        private LinearLayout ll_emotion;
        private TextView tv_danger;
        private LinearLayout ll_danger;

        public MyViewHolder(View view)
        {
            super(view);
            pr_no = (TextView)view.findViewById(R.id.tv_pr_no);
            pr_name = (TextView)view.findViewById(R.id.tv_pr_name);
            visittype = (TextView)view.findViewById(R.id.tv_visittype);
            prenum = (TextView)view.findViewById(R.id.tv_prenum);
            state = (TextView)view.findViewById(R.id.tv_state);
            visitor_info = (TextView)view.findViewById(R.id.tv_visitor_info);
            startdate = (TextView)view.findViewById(R.id.tv_startdate);
            enddate = (TextView)view.findViewById(R.id.tv_enddate);
            ll_rootView = (LinearLayout) view.findViewById(R.id.ll_rootView);
            v_line = view.findViewById(R.id.v_line);
            tv_remark = (TextView)view.findViewById(R.id.tv_remark);
            ll_remark = (LinearLayout)view.findViewById(R.id.ll_remark);
            tv_emotion = (TextView)view.findViewById(R.id.tv_emotion);
            ll_emotion = (LinearLayout)view.findViewById(R.id.ll_emotion);
            tv_danger = (TextView)view.findViewById(R.id.tv_danger);
            ll_danger = (LinearLayout)view.findViewById(R.id.ll_danger);
        }
    }


    public interface OnItemClickListener{
        void OnItemClick(int position, Response_meetRecord_item item);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
